#import "Level3Layer.h"

@implementation Level3Layer

-(id) init
{
    if( (self = [super init]) )
    {       
        CGSize winSize = [self contentSize];
        
        //Create the background image 
        CCSprite *background = [CCSprite spriteWithFile:@"Default.png"];
        [background setPosition:CGPointMake(winSize.width/2, winSize.height/2)];
        //[self addChild:background z:0];
    }
    
    return self;
}

-(void) dealloc
{
    [super dealloc];
}

#pragma mark Overrided Function
-(void) createSprites:(NSMutableArray *)numberArray wave:(int)wave
{
	int iSpritesCount = [numberArray count];
    CGSize winSize = [self contentSize];

    int rowCount = iSpritesCount / 8;   // 16/8 = 2 row
    int columnCount = iSpritesCount / 4;    //16/4 = 4 column
    
    int gridWidth = winSize.width/columnCount;
    int gridHeight = winSize.height/rowCount;
    
    int i=0;
    for(int row=0; row<rowCount; row++)
    {
        for(int column=0; column<columnCount; column++)
        {
            for(int k=0; k<2; k++)
            {
                int num = [[numberArray objectAtIndex:i] intValue];
                i++;
                
                LanternSprite *newSprite = (LanternSprite *)[self createSpriteWithType:kSPRITE_TYPE_LANTERN number:num];
                
                [newSprite setScale:0.7];
                newSprite.position = ccp(column * gridWidth + gridWidth/2,
                                         row * gridHeight + gridHeight*2/3);
                [newSprite setDelegate:self];

                CCRotateBy *action1 = [CCRotateBy actionWithDuration:0.3 angle:-30];
                CCRotateBy *action2 = [CCRotateBy actionWithDuration:0.6 angle:60];
                CCRotateBy *action3 = [CCRotateBy actionWithDuration:0.3 angle:-30];
                [newSprite runAction:[CCRepeatForever actionWithAction:[CCSequence actions:action1, action2, action3, nil]]];
                
                [self addChild:newSprite z:-num];   //Topmost for smallest number
            }
        }
    }
}

-(void) startLevel
{
	
}

-(void) startWave:(int)wave
{
	//Ask Sprite to start action
	for(int i=0; i< [self.spritesArray count]; i++)
	{
		GeneralSprite *sprite = [self.spritesArray objectAtIndex:i];
		[sprite startActionWithWave:wave];
	}
}

-(void) willCompleteLevel
{
	
}

-(void) willCompleteWave
{
	
}

-(void) startPerformCorrectAnswer:(GeneralSprite *)sprite
{
	//TODO: Perform any Layer correct action
	
	//Perform Sprite Action
	[sprite startCorrectEffect];
}

-(void) startPerformIncorrectAnswer:(GeneralSprite *)sprite
{
	//TODO: Perform any Layer correct action
	
	//Perform Sprite Action
	[sprite startIncorrectEffect];
}

/*
#pragma mark LanternSpriteDelegate
-(void) lanternSpriteTouchBegan:(LanternSprite *)sender
{
    [delegate checkAnswer:sender];
    
    if(bIsCorrect)
    {
        [sender startCorrectEffect];
    }
    else
    {
        [delegate deductTime];
        [sender startIncorrectEffect];
    }
}

-(void) lanternSpriteTouchMoved:(LanternSprite *)sprite location:(CGPoint)location
{
}

-(void) lanternSpriteTouchEnd:(LanternSprite *)sprite
{
    
}

-(void) lanternStartCorrectEffect:(LanternSprite *)sender
{
}

-(void) lanternFinishCorrectEffect:(LanternSprite *)sender
{
    CGPoint pos = sender.position;
    
    //Remove the Sprite if correct 
    [self removeSprite:sender];
    
    BOOL bIsWaveCompleted = [delegate isWaveComplete];
    if(bIsWaveCompleted)
    {
        //Check if level comepleted
        BOOL bIsLevelCompleted = [delegate isLevelComplete];
        
        if(bIsLevelCompleted)
        {
            [delegate pauseTimer];
            [self showGuGu:pos];
        }
        else
        {
            //Another wave
            [delegate waveCompleted];
        }
    }
}

-(void) lanternStartIncorrectEffect:(LanternSprite *)sender
{
    CCArray *children = [self children];
    
    for(int i=0; i<[children count]; i++)
    {
        if( [[children objectAtIndex:i] isKindOfClass:[LanternSprite class]] )
        {
            LanternSprite *sprite = [children objectAtIndex:i];
            [sprite stopAllActions];
            [sprite unscheduleUpdate];
            
            CCMoveTo *action1 = [CCMoveTo actionWithDuration:2 position:ccp(sprite.position.x, -100)];
            [sprite runAction:[CCSequence actions:action1, nil]];
        }
    }

}

-(void) lanternFinishIncorrectEffect:(LanternSprite *)sender
{
}
*/
@end
